package com.yf.course.modules.course.service.impl;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.base.api.api.dto.PagingReqDTO;
import com.yf.base.utils.DateUtils;
import com.yf.course.modules.course.dto.CourseLearnDailyDTO;
import com.yf.course.modules.course.entity.CourseFaceRecord;
import com.yf.course.modules.course.entity.CourseLearnDaily;
import com.yf.course.modules.course.mapper.CourseLearnDailyMapper;
import com.yf.course.modules.course.service.CourseLearnDailyService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

/**
* <p>
* 学习日统计业务实现类
* </p>
*
* @author 聪明笨狗
* @since 2022-03-29 11:29
*/
@Service
public class CourseLearnDailyServiceImpl extends ServiceImpl<CourseLearnDailyMapper, CourseLearnDaily> implements CourseLearnDailyService {



    @Override
    public IPage<CourseLearnDailyDTO> paging(PagingReqDTO<CourseLearnDailyDTO> reqDTO) {

        //查询条件
        QueryWrapper<CourseLearnDaily> wrapper = new QueryWrapper<>();

        // 请求参数
        CourseLearnDailyDTO params = reqDTO.getParams();
        if(params!=null){
            if(StringUtils.isNoneBlank(params.getCourseId())){
                wrapper.lambda().eq(CourseLearnDaily::getCourseId, params.getCourseId());
            }
            if(StringUtils.isNoneBlank(params.getUserId())){
                wrapper.lambda().eq(CourseLearnDaily::getUserId, params.getUserId());
            }
            if(StringUtils.isNoneBlank(params.getFileId())){
                wrapper.lambda().eq(CourseLearnDaily::getFileId, params.getFileId());
            }
        }

        wrapper.lambda().orderByDesc(CourseLearnDaily::getDay);

        //获得数据
        IPage<CourseFaceRecord> page = this.page(reqDTO.toPage(), wrapper);
        //转换结果
        IPage<CourseLearnDailyDTO> pageData = JSON.parseObject(JSON.toJSONString(page), new TypeReference<Page<CourseLearnDailyDTO>>(){});
        return pageData;
    }

    @Override
    public void incr(String courseId, String fileId, String userId, long sec) {

        QueryWrapper<CourseLearnDaily> wrapper = new QueryWrapper<>();
        wrapper.lambda()
                .eq(CourseLearnDaily::getCourseId, courseId)
                .eq(CourseLearnDaily::getFileId, fileId)
                .eq(CourseLearnDaily::getDay, DateUtils.dateNow("yyyy-MM-dd"))
                .eq(CourseLearnDaily::getUserId, userId);

        CourseLearnDaily learn = this.getOne(wrapper, false);


        if(learn==null){
            learn = new CourseLearnDaily();
            learn.setCourseId(courseId);
            learn.setFileId(fileId);
            learn.setDay(DateUtils.dateNow("yyyy-MM-dd"));
            learn.setUserId(userId);
            learn.setLearnSec(sec);
            this.save(learn);
        }else{
            learn.setLearnSec(learn.getLearnSec() + sec);
            this.updateById(learn);
        }
    }

    @Override
    public long daily(String courseId,  String userId) {
        QueryWrapper<CourseLearnDaily> wrapper = new QueryWrapper<>();
        wrapper.select("SUM(learn_sec) AS learn_sec");
        wrapper.lambda()
                .eq(CourseLearnDaily::getCourseId, courseId)
                .eq(CourseLearnDaily::getDay, DateUtils.dateNow("yyyy-MM-dd"))
                .eq(CourseLearnDaily::getUserId, userId);

        CourseLearnDaily learn = this.getOne(wrapper, false);
        if(learn!=null){
            return learn.getLearnSec();
        }
        return 0L;
    }
}
